<template>
    <div class="base" v-show="isShow">
        <div class="mask">

            <el-container class="login">
                <el-main>
                    <el-form ref="form" :model="form" :rules="rules">
                        <i @click="setIsShow(!isShow)" style="float: left;" class="el-icon-close close"></i>
                        <el-form-item>
                            <img class="img" src="../assets/login-logo-6f72f64ae3.png" alt="">
                        </el-form-item>
                        <el-form-item ref="phone" prop="phone">
                            <el-input v-model="form.phone" class="my-input" placeholder="手机号"></el-input>
                        </el-form-item>
                        <el-form-item v-show="!isPwdLogin" ref="imgCode" prop="imgCode">
                            <el-input v-model="form.imgCode" class="my-input" placeholder="图片验证码"></el-input>
                            <div @click="getImgCode" class="imgCode" v-html="imgCode"></div>
                        </el-form-item>
                        <el-form-item v-show="!isPwdLogin" prop="phoneCode">
                            <el-input v-model="form.phoneCode" class="my-input" placeholder="手机验证码"></el-input>
                            <div ref="phoneCode" class="phoneCode" @click="getPhoneCode">获取验证码</div>
                        </el-form-item>
                        <!-- 密码输入 -->
                        <el-form-item prop="password" v-show="isPwdLogin">
                            <el-input show-password v-model="form.password" class="my-input" placeholder="密码"></el-input>
                        </el-form-item>
                        <el-form-item :style="{ position: 'relative' }">
                            <el-button @click="doLogin">登录</el-button>
                            <span class="byPwdLog" @click="isPwdLogin = !isPwdLogin" v-if="!isPwdLogin">密码登录</span>
                            <span class="byPwdLog" @click="isPwdLogin = !isPwdLogin" v-else="isPwdLogin">手机验证码登录</span>

                        </el-form-item>

                        <el-form>
                            <span :style="{ padding: `0 50px`, fontSize: '14px' }">
                                未注册过的手机号将自动创建摩天轮账号，且代表您已同意
                                <a href="">《摩天轮用户信息及隐私保护政策》</a>
                                和
                                <a href="">《摩天轮平台交易服务协议》</a>
                            </span>
                        </el-form>
                    </el-form>
                </el-main>
            </el-container>
        </div>
    </div>
</template>

<script>
import { mapMutations, mapState } from 'vuex';
import httpApi from '@/http';
import { CheckboxGroup } from 'element-ui';

export default {
    data() {
        return {
            imgCode: "",//图片验证码
            str: "",//图片验证码结果
            form: {
                phone: "",//手机号
                imgCode: "",//图片验证码
                phoneCode: "",//手机验证码
                password: ""//密码
            },
            rules: {
                phone: [{ required: true, message: "手机号不能为空", trigger: "blur" },
                { pattern: /^1[3-9]\d{9}$/, message: "手机号格式错误", trigger: "blur" },],
                imgCode: [{ required: true, message: "图片验证码错误", trigger: "blur" }],
                phoneCode: [{ required: true, message: "手机验证码不能为空", trigger: "blur" }],
                password: [{ required: true, message: "密码不能为空", trigger: "blur" },
                { pattern: /\w{6,}$/, message: "密码至少为6位", trigger: "blur" },],
            },
            isPwdLogin: false// 是否位密码登录
        }
    },
    computed: {
        ...mapState(['isShow'])
    },
    methods: {

        ...mapMutations(['setIsShow', 'setToken', 'setUserInfo']),
        /**
         * 获取图片验证码
         */
        async getImgCode() {
            let { data: { code, data: { img, str } } } = await httpApi.usersApi.queryImgCode();
            console.log(code);
            if (code == 200) {
                this.imgCode = img;
                this.str = str;
            }
        },
        /**
         * 获取手机验证码
         */
        async getPhoneCode() {
            this.$refs.form.validateField(['phone', 'imgCode'], async (err) => {
                if (!err) {
                    if (this.form.imgCode.toUpperCase() !== this.str) {
                        this.form.imgCode = ""
                        return;
                    }
                    // 发送手机验证码
                    let params = { phone: this.form.phone }
                    httpApi.usersApi.queryPhoneCode(params);
                }
            })
        },
        /**
         * 登录
         */
        async doLogin() {
            // 采用手机验证码登录
            if (!this.isPwdLogin) {
                this.$refs.form.validateField(['phone', 'imgCode', 'phoneCode'], async (err) => {
                    if (!err) {
                        let params = { phone: this.form.phone, code: this.form.phoneCode }
                        let { data: { code, data: { token, user } } } = await httpApi.usersApi.loginByCode(params);
                        if (code == 200) {
                            this.setToken(token);
                            this.setUserInfo(user[0]);
                            this.setIsShow(false);
                        } else {
                            // 登录失败，将验证码清空
                            this.form.imgCode = "";
                            this.form.phoneCode = "";
                        }
                    }
                })
            } else {
                // 采用密码登录
                this.$refs.form.validateField(['phone', 'password'], async (err) => {
                    if (!err && this.form.password.length >= 6) {
                        debugger;
                        let params = { phone: this.form.phone, password: this.form.password }
                        let { data: { code, data: { token, user } } } = await httpApi.usersApi.loginByAccount(params);
                        if (code == 200) {
                            this.setToken(token);
                            this.setUserInfo(user[0]);
                            this.setIsShow(false);

                        } else {
                            // 登录失败，将验证码清空
                            this.form.password = "";
                        }
                    }
                })
            }
        }
    },
    mounted() {
        this.getImgCode({});
    },
}
</script>

<style lang="scss" scoped>
.byPwdLog {
    position: absolute;
    top: 100%;
    left: 50%;
    transform: translate(-50%, -20%);
    cursor: pointer;
    user-select: none;

    &:hover {
        color: darken(rgb(103, 105, 103), 0.1);
    }
}

.imgCode,
.phoneCode {
    position: absolute;
    width: 40%;
    height: 100%;
    right: 0;
    top: 0;
    background-color: #ccc;
    border-radius: 0 4px 4px 0;
    cursor: pointer;
    user-select: none;
}

.imgCode {
    ::v-deep svg {
        width: 100%;
        height: 100%;
    }
}

.phoneCode {
    text-align: center;
    color: #756d6a;
}

.login {
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: #fff;
    border-radius: 8px;

}

.mask {
    width: 100vw;
    height: 100vh;
    background-color: rgba(0, 0, 0, 0.5);
    position: fixed;
    top: 0;
    left: 0;
}

.close {
    position: absolute;
    top: 0;
    right: 0;
    transform: translate(-100%, 100%);
    cursor: pointer;
    font-size: 1.2rem;

    &:hover {
        color: #ccc;
    }
}

.el-form {
    display: flex;
    flex-direction: column;
    width: 400px;
    // height: 410px;
    margin: auto;
    // border: 1px solid red;
    border-radius: 8px;
}

.el-form-item {
    display: flex;
    justify-content: center;
}

.el-button {
    width: 280px;
    height: 40px;
}

.img {
    width: 160px;
    height: 70px;
    display: block;
    margin: auto;
}

.el-input {
    width: 280px;
    height: 40px;
    // padding: 0 15px;
    border: 1px solid #f7f7f7;
    border-radius: 8px;
}
</style>